/*
 * @Author: songliuyang
 * @Date: 2021-10-Th 06:09:56
 * @Last Modified by:   songlyyes@163.com
 * @Last Modified time: 2021-10-Th 06:09:56
 */

export * from './typeCheck'
export * from './dateUtils'
/**
 * 复制内容到粘贴板
 * @param {String} content : 需要复制的内容
 * @returns null
 * @example copyToClip('xxx')
 */
export function copyToClip (content) {
  var aux = document.createElement('input')
  aux.setAttribute('value', content)
  document.body.appendChild(aux)
  aux.select()
  document.execCommand('copy', false)
  document.body.removeChild(aux)
}

/**
 * 获取文件后缀名
 * @param {String} filename
 * @returns {string}
 * @example getExt("1.mp4")
 */
export function getExt (filename) {
  if (typeof filename === 'string') {
    return filename
      .split('.')
      .pop()
      .toLowerCase()
  } else {
    throw new Error('filename must be a string type')
  }
}

/**
 * 休眠xxxms
 * @param {Number} milliseconds
 * @example const fetchData = async () => { await sleep(1000) }
 */
export function sleep (ms) {
  return new Promise(resolve => setTimeout(resolve, ms))
}

/**
 * 生成随机id
 * @param {*} length
 * @param {*} chars
 * @returns {string}
 * 第一个参数指定位数，第二个字符串指定字符，都是可选参数，如果都不传，默认生成8位
 */
export function uuid (length, chars) {
  chars =
        chars ||
        '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
  length = length || 8
  var result = ''
  for (var i = length; i > 0; --i) { result += chars[Math.floor(Math.random() * chars.length)] }
  return result
}

/**
 *深拷贝
 * @export
 * @param {*} obj
 * // 缺陷：只拷贝对象、数组以及对象数组，对于大部分场景已经足够
 * // const person = { name: 'xiaoming', child: { name: 'Jack' } }
 * // deepCopy(person)
 * @returns {Obj}
 */
export function deepCopy (obj) {
  if (typeof obj !== 'object') {
    return obj
  }
  if (obj == null) {
    return obj
  }
  return JSON.parse(JSON.stringify(obj))
}

/**
 * 数组去重
 * @param {*} arr
 * @returns {Array}
 */
export function uniqueArray (arr) {
  if (!Array.isArray(arr)) {
    throw new Error('The first parameter must be an array')
  }
  if (arr.length == 1) {
    return arr
  }
  return [...new Set(arr)]
}

/**
 * 对象转化为formdata
 * @param {Object} object
 * @returns {formData} object
 */
export function toFormData (object) {
  const formData = new FormData()
  Object.keys(object).forEach(key => {
    const value = object[key]
    if (Array.isArray(value)) {
      value.forEach((subValue, i) =>
        formData.append(key + `[${i}]`, subValue)
      )
    } else {
      formData.append(key, object[key])
    }
  })
  return formData
}
/**
 * 对象转化为formdata
 * @param {Object} obj
 * @returns { String } obj
 */
export const toQueryParams = (obj) => {
  let str = ''
  for (const key in obj) {
    if (str !== '') {
      str += '&'
    }
    if (key) {
      str += key + '=' + encodeURIComponent(obj[key])
    }
  }
  return str
}

/**
 * 保留小数点以后几位，默认2位
 * @param { Number } number
 * @returns { Number }
 */
export function cutNumber (number, no = 2) {
  if (typeof number !== 'number') {
    number = Number(number)
  }
  return Number(number.toFixed(no))
}

/**
 * 生成一个指定范围内的随机整数
 * @param { Number } minNum
 * @param { Number } maxNum
 * @returns { Number }
 */
export function getRandomNum (minNum, maxNum) {
  if (typeof minNum !== 'number') {
    minNum = Number(minNum) || 0
  }
  if (typeof maxNum !== 'number') {
    maxNum = Number(maxNum) || 100
  }
  //  Math.random() * maxNum [0, maxNum)
  // Math.floor(Math.random() * (maxNum + 1)) // [0 maxNum]
  return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum) // [minNum, maxNum]
}

/**
 * 获取url中指定参数名的参数值
 * @param { String } name
 * @returns { String }
 */
export function getUrlParams (name) {
  const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  const r = window.location.search.substr(1).match(reg)
  if (r != null) return decodeURI(r[2])
  return null
}
